//
//  MJLoginViewModel.m
//  MJ
//
//  Created by qiaoBu on 2017/12/11.
//  Copyright © 2017年 lina. All rights reserved.
//

#import "MJLoginViewModel.h"
#import "MJNetWorkTool.h"

@implementation MJLoginViewModel

+ (void)requestLoginWithDict:(NSMutableDictionary *)parameters  success:(void (^)(NSString *state,NSString *message))requestSuccess failure:(void(^)(NSError *error))requestFailure netWorkState:(void (^) (BOOL netWorkState))netWorkState{
    
    [[MJNetWorkTool sharedUtil] requestAFURL:MJ_Me_Login httpMethod:1 parameters:parameters succeed:^(id dict) {
        
       NSDictionary *resultDict = [NSDictionary dictionaryWithDictionary:dict];
        NSString *stateCode = [resultDict objectForKey:@"flag"];
        
        if ([stateCode isEqualToString:@"001"])
        {
            NSString *userTicket = [NSString stringWithFormat:@"%@",[[resultDict objectForKey:@"content"] objectForKey:@"ticket"]];
        
            [[NSUserDefaults standardUserDefaults] setObject:userTicket forKey:MJ_Ticket];
            [[NSUserDefaults standardUserDefaults] setBool:YES forKey:IsLogined];
            [[NSUserDefaults standardUserDefaults] synchronize];
            requestSuccess(stateCode,nil);
        }
        else
        {
            requestSuccess(stateCode,[resultDict objectForKey:@"content"]);
        }
    
    } failure:^(NSError *err) {
        requestFailure (err);
    }];
}

+ (void)requestCodeWithDict:(NSMutableDictionary *)parameters success:(void (^)(NSString *state,NSString *message))requestSuccess failure:(void(^)(NSError *error))requestFailure netWorkState:(void (^) (BOOL netWorkState))netWorkState{
    
    [[MJNetWorkTool sharedUtil] requestAFURL:MJ_GetCode httpMethod:1 parameters:parameters succeed:^(id dict) {
        
        NSDictionary *resultDict = [NSDictionary dictionaryWithDictionary:dict];
        NSString *stateCode = [resultDict objectForKey:@"flag"];
        
        requestSuccess(stateCode,[resultDict objectForKey:@"content"]);
        
    } failure:^(NSError *err) {
        requestFailure (err);
    }];
    
}
@end
